<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
        var spiralOrder = function (matrix) {
          /*
          这题其实和螺旋矩阵2是一样的
          */
          let res = []
          let m = matrix.length
          let n = matrix[0].length
          let min = Math.min(m, n)
          //定义循环次数
          let loop = parseInt(min / 2)
          //定义偏移量
          let offset = 1
          //定义下标
          let startX = 0
          let startY = 0
          while (loop--) {
            let i = startX
            let j = startY

            for (; j < n - offset; j++) {
              res.push(matrix[i][j])
            }
            for (; i < m - offset; i++) {
              res.push(matrix[i][j])
            }
            for (; j > startY; j--) {
              res.push(matrix[i][j])
            }
            for (; i > startX; i--) {
              res.push(matrix[i][j])
            }
            offset++
            startX++
            startY++
          }
          //找出最小边
          if (min % 2) {
            //说明剩下一行或者一列
            //剩下一列
            if (min == n) {
              for (let p = startX; p <= m - offset; p++) {
                res.push(matrix[p][startY])
              }
            } else {
              for (let q = startY; q <= n - offset; q++) {
                res.push(matrix[startX][q])
              }
            }
          }
          return res
        }
      console.log(spiralOrder([[3], [2]]))
    </script>
  </body>
</html>
